﻿using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;

namespace Com.OpenCode.Framework.DevForm
{
    public class ToolBarComboxUI : BarEditItem, IToolBarMenuProperty
    {
        private ICommand _Command;
        private RepositoryItemComboBox _ComboBox;

        public ToolBarComboxUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            IToolBarMenuProperty _ToolBarMenuProperty = args.Owner as IToolBarMenuProperty;
            OwnerToolBarMenuUI = _ToolBarMenuProperty.OwnerToolBarMenuUI;
            _ComboBox = new RepositoryItemComboBox();
            Edit = _ComboBox;
            _ComboBox.SelectedValueChanged += ComboBox_SelectedValueChanged;
            if (args.Plugin.Properties.Exist("Width"))
            {
                Width = args.Plugin.Properties.GetInt("Width");
            }
            if (args.Plugin.Properties.GetBool("ReadOnly"))
            {
                _ComboBox.TextEditStyle= TextEditStyles.DisableTextEditor;
            }
            Plugin commandPlugin = args.Plugin.Addin.Plugin.Search("Class", "Id", args.Plugin.Properties.GetString("Image"));
            if (commandPlugin != null)
            {
                _Command = (ICommand)args.Plugin.Addin.Bulid(commandPlugin, null);
            }
            bool _BeginGroup = false;
            bool.TryParse(args.Plugin.Properties.Get("BeginGroup").ToString(), out _BeginGroup);
            _ToolBarMenuProperty.BeginGroup = _BeginGroup;
            _ToolBarMenuProperty.Add(this);
        }

        private void ComboBox_SelectedValueChanged(object sender, System.EventArgs e)
        {
            if (_Command != null)
            {
                _Command.Run();
            }
        }

        public RepositoryItemComboBox ComboBox
        {
            get { return _ComboBox; }
        }

        public OpenCodeToolBarMenuUI OwnerToolBarMenuUI { get; set; }
        public bool BeginGroup { get; set; }
        public void Add(BarItem item)
        {
            
        }
    }
}
